package com.yw.atp.action;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.yw.atp.model.Weather;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.service.WeatherService;
import com.yw.atp.util.DateUtil;
import com.yw.atp.util.StringUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("weatherAction")
public class WeatherAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
	private PersistenceService service;
	@Autowired
	private WeatherService weatherService;
	HttpServletRequest request;
	public List<Weather> weatherList;
	public Weather weather;
	public String weatherIds;
	public String res;
	public int start = 0;
	public int limit = 20;

	public String list(){
		if(weather!=null&&weather.getDate()!=null){
			try {
				weatherList = weatherService.getWeathers(weather.getDate());
			} catch (ParseException e) {
				weatherList = service.findAll(Weather.class,0);
			}
		}else{
			weatherList = service.findAll(Weather.class,0);
		}
		JSONObject js = new JSONObject();
		try {
			if(weatherList!=null&&weatherList.size()>0){
				js.put("totalCount",weatherList.size());
				int len = weatherList.size();
				start = start>(len-1)?(len-1):start;
				limit = limit>(len-start)?(len):(start+limit);
				js.put("topics",Weather.capture(weatherList.subList(start,limit)));
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String add(){
		if(StringUtil.checkWeather(weather)){
			weather.setUpdateTime(DateUtil.getClientFormatA(new Date()));
			if(weather.getId()!=null&&weather.getId().intValue()>0){
				Weather w = service.load(weather);
				weather.setVersion(w.getVersion()+1);
			}else{
				weather.setCreatedAt(new Date());
			}
			service.save(weather);
			return SUCCESS;
		}else{
			return INPUT;
		}
	}
	
	public String del(){
		if(weatherIds!=null&&!"".equals(weatherIds)){
			service.remove(Weather.class.getName(),weatherIds);
		}
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public PersistenceService getService() {
		return service;
	}
	
	public void setService(PersistenceService service) {
		this.service = service;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public List<Weather> getWeatherList() {
		return weatherList;
	}

	public void setWeatherList(List<Weather> weatherList) {
		this.weatherList = weatherList;
	}

	public Weather getWeather() {
		return weather;
	}

	public void setWeather(Weather weather) {
		this.weather = weather;
	}

	public String getWeatherIds() {
		return weatherIds;
	}

	public void setWeatherIds(String weatherIds) {
		this.weatherIds = weatherIds;
	}
	
	public WeatherService getWeatherService() {
		return weatherService;
	}

	public void setWeatherService(WeatherService weatherService) {
		this.weatherService = weatherService;
	}
}
